import 'package:capsule/widgets/dialog/dialog_template.dart';
import 'package:capsule/widgets/form/form_input.dart';

import 'package:flutter/material.dart';

class InputDialog extends StatefulWidget {
  final void Function(String value)? onOk;
  final void Function(String value, String replacement)? onReplace;
  final void Function()? onCancel;
  final String prompt;
  final String? prompt2;
  const InputDialog(
      {super.key,
      this.onOk,
      this.onCancel,
      this.prompt = "请输入：",
      this.prompt2,
      this.onReplace});

  @override
  State<InputDialog> createState() => _InputDialogState();
}

class _InputDialogState extends State<InputDialog> {
  late final TextEditingController _inputTextController;
  TextEditingController? _replacementController;
  final GlobalKey<FormState> _inputFormKey = GlobalKey();
  final GlobalKey<FormState> _replacementInputFormKey = GlobalKey();
  @override
  void initState() {
    super.initState();

    _inputTextController = TextEditingController();
    if (widget.prompt2 != null) {
      _replacementController = TextEditingController();
    }
  }

  void _onOK() {
    if (_inputFormKey.currentState!.validate() == false) return;
    if (widget.prompt2 != null) {
      if (_replacementInputFormKey.currentState!.validate() == false) {
        return;
      }
    }
    if (widget.onReplace != null && widget.prompt2 != null) {
      widget.onReplace
          ?.call(_inputTextController.value.text, _replacementController!.text);
    } else {
      widget.onOk?.call(_inputTextController.value.text);
    }
  }

  @override
  Widget build(BuildContext context) {
    double dlgWidth = 560;
    return DialogTemplate(
      onCancel: widget.onCancel,
      onOk: _onOK,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(widget.prompt),
          const SizedBox(
            height: 5,
          ),
          Form(
            key: _inputFormKey,
            child: SizedBox(
              width: dlgWidth - 40,
              child: FormInput(
                controller: _inputTextController,
                onFieldSubmitted: (value) {
                  _onOK();
                },
                validator: (value) {
                  if (value == null || value.isEmpty) {
                    return "不能为空";
                  }
                  return null;
                },
              ),
            ),
          ),
          if (widget.prompt2 != null) ...[
            const SizedBox(
              height: 5,
            ),
            Text(widget.prompt2!),
            const SizedBox(
              height: 5,
            ),
            Form(
              key: _replacementInputFormKey,
              child: SizedBox(
                width: dlgWidth - 40,
                child: FormInput(
                  controller: _replacementController,
                  validator: (value) {
                    if (value == null || value.isEmpty) {
                      return "不能为空";
                    }
                    return null;
                  },
                ),
              ),
            )
          ]
        ],
      ),
    );
  }
}
